package com.etc.controler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.service.impl.UserServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

@WebServlet("/user/list")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService us = new UserServiceImpl();
    
	//浏览器可以直接调用  或者  layui-list.jsp可以调用
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    //设置响应编码
		response.setCharacterEncoding("UTF-8");
		
		//设置json格式
		response.setContentType("application/json");
		
		Integer page = 1;
		Integer limit = 10;
		String content = "";
		if(request.getParameter("page")!=null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("limit")!=null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}
		if(request.getParameter("content")!=null) {
			content = request.getParameter("content");
		}
		
		
		PageData<User> pd = us.getUserByPage(page, limit, content);
		
		pd.setCode(0);
		pd.setMsg("请求成功");
		
		Gson gson = new Gson();
		String str = gson.toJson(pd);
		
		PrintWriter out = response.getWriter();
		out.print(str);
		out.close();
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
